// pages/myVisitor/myVisitor.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req: false,
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    list: [],
    refresh: true
  },


  list: function(append) {
    var that = this;
    that.toggle("req", false);
    var tabActive = that.data.active;
    var param = that.data.param
    app.req({
      url: 'orgmini/FriendVisitor/visitorList',
      data: param,
      success: function(res) {
        if (200 == res.code) {

          var list = res.data.rows;
          if (!list || list.length < param.pageSize) {
            that.toggle("pageEnd", true);
          }
          that.toggle("list", append ? that.data.list.concat(list) : list);
          that.toggle("req", true);
        }
      }
    });
  },

  readAll: function() {
    app.req({
      url: 'orgmini/FriendVisitor/readAll',
      success: function(res) {
        if (200 == res.code) {

        }
      }
    });
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    this.toggle('refresh', false)
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn + "?type=" + dataset.type + '&id=' + dataset.id + '&name=' + dataset.name
    // if(dataset.type){
    //   url = url + "?type=" + dataset.type
    // }
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.readAll()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    if (that.data.refresh) {
      app.getUserInfo(function(userInfo) {
        that.toggle("param.uid", userInfo.id);
        that.list()
      });
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.list();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      that.list(true);
    }
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})